Page({
  data: {
    remote_src: getApp().globalData.remote_src,
    activityInfo: null,
    showCancelModal: false
  },
  checkMoreActivity() {
  console.log(this.data.activityInfo)
    wx.navigateTo({
      url: '/pages/home/activity/activity?id=' + this.data.activityInfo.id,
      // url: `/pages/home/activity/activity?id=${activityId}`,
    });
  },
  onCancelActivity() {
    this.setData({
      showCancelModal: true
    });
  },

  closeCancelModal() {
    this.setData({
      showCancelModal: false
    });
  },
  confirmCancelActivity() {
    const websiteUrl = getApp().globalData.onlineService + '/api/activity/activity/cancel';
    wx.request({
      url: websiteUrl,
      method: 'POST',
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${getApp().globalData.userInfo.token}`
      },
      data: {
        activity_id: parseInt(this.data.activityInfo.id)
      },
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            showCancelModal: false,
            'activityInfo.status': '3'  // 更新活动状态为已取消
          });
           // 获取上一页实例
           const pages = getCurrentPages();
           const prevPage = pages[pages.length - 2];
           
           // 更新上一页的活动列表
           if (prevPage) {
             // 从列表中移除已取消的活动
             const updatedList = prevPage.data.createdList.filter(
               activity => activity.id !== this.data.activityInfo.id
             );
             console.log(updatedList);
             prevPage.setData({
              createdList: updatedList,
              createdCount: updatedList.length // 更新活动数量
             });
           }
          wx.showToast({
            title: '活动已取消',
            icon: 'success'
          });
          setTimeout(() => {
            wx.navigateBack();
          }, 1500);
        } else {
          wx.showToast({
            title: res.data.msg || '取消失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        });
      }
    });
  },
  goToHome() {
    wx.switchTab({
      url: '/pages/publish/publish'
    });
  },
  onLoad(options) {
    // 获取活动详情
    const decodedOptions = {};
    for (let key in options) {
      if (key === 'address' || key === "images") {
        decodedOptions[key] = JSON.parse(decodeURIComponent(options[key]));
      } else {
        decodedOptions[key] = decodeURIComponent(options[key]);
      }
    }
    console.log(decodedOptions);
    this.setData({
      activityInfo: decodedOptions
    });
  },
  // 修改活动
  onEditActivity() {
      const activity = this.data.activityInfo;
      // 将活动信息转换为查询字符串
      const queryString = Object.keys(activity)
        .map(key => {
          let value;
          if (key === 'address') {
            value = encodeURIComponent(JSON.stringify(activity[key]));
          } else if (key === 'images') {
            value = encodeURIComponent(JSON.stringify(activity[key])); // 将图片数组转为 JSON 字符串
          } else {
            value = encodeURIComponent(activity[key]);
          }
          return `${key}=${value}`;
        })
        .join('&');
      
      wx.navigateTo({
        url: `/pages/mine/my_created_activity/activity/activity?${queryString}`
      });
  },
  onShareActivity() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },
  openLocation() {
    const address = this.data.activityInfo.address;
    wx.openLocation({
      latitude: parseFloat(address.latitude),
      longitude: parseFloat(address.longitude),
      name: address.name,
      address: address.address,
      scale: 18
    });
  },
  onStopInvite() {
    const websiteUrl = getApp().globalData.onlineService + '/api/activity/activity/start';
    wx.request({
      url: websiteUrl,
      method: 'POST',  // 改为 POST 方法
      header: {
        'content-type': 'application/json',
        'Authorization': `Bearer ${getApp().globalData.userInfo.token}`
      },
      data: {
        activity_id: parseInt(this.data.activityInfo.id)
      },
      success: (res) => {
        if (res.data.code === 200) {
          this.setData({
            'activityInfo.status': '1'
          });
          wx.showToast({
            title: '活动已开始',
            icon: 'success'
          });
          // 可以跳转到群聊页面
          // if (res.data.data && res.data.data.group_id) {
          //   wx.navigateTo({
          //     url: `/pages/chat/chat?group_id=${res.data.data.group_id}`
          //   });
          // }
        } else {
          wx.showToast({
            title: res.data.msg || '操作失败',
            icon: 'none'
          });
        }
      },
      fail: (err) => {
        wx.showToast({
          title: '网络错误',
          icon: 'error'
        });
      }
    });
  }
});